import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @author cino
 * justCode
 * 统计文章字数
 */
public class splitTest {
    @Test
    public void Test(){
        String s = "how many words there?what happen?no,that's not";
        String s2 = "how many words are there in this sentence? how many times do these words appear?";

        String s1 = "这里有多少个字？统计一下，这里的字数。";
        // EnglishWords(s);
        // ChineseWords(s1);
        howmanyWords(s2);
    }


    public void EnglishWords(String s){
        String s1 = s.replace("?", " ");
        System.out.println(s1);
        String s2 = s1.replace(",", " ");
        System.out.println(s2);
        String[] s3 = s2.split(" ");
        System.out.println(s3.length);
    }


    public void howmanyWords(String s){
        String replace = s.replace("?", " ");
        // 将字符串按照空格分割成单词数组
        String[] words = replace.split(" ");
        // System.out.println(words[7]);

        // 创建 HashMap 存储单词和出现次数的对应关系
        Map<String, Integer> wordCountMap = new HashMap<>();


        for (String word : words) {
            // 判断单词是否已存在于 HashMap 中，如果存在则更新计数，否则添加到 HashMap
            if (wordCountMap.containsKey(word)) {
                int count = wordCountMap.get(word);
                wordCountMap.put(word, count + 1);
            } else {
                wordCountMap.put(word, 1);
            }
        }

        for (Map.Entry<String, Integer> entry : wordCountMap.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }

    public void ChineseWords(String s){
        String s1 = s.replace("？", "");
        System.out.println(s1);
        String s2 = s1.replace("，", "");
        System.out.println(s2);
        String s3 = s2.replace("。", "");
        System.out.println(s3);
        System.out.println(s3.length());
    }
}
